package com.example.task;

import com.example.mapper.*;
import com.example.mapper.pc.ResourceMapper;
import com.example.service.ZnxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(Integer.MAX_VALUE)
public class MaintainTask {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private MaintainplanMapper maintainplanMapper;
    @Autowired
    private MaintainplanruleMapper maintainplanruleMapper;
    @Autowired
    private MaconlineinfoMapper maconlineinfoMapper;
    @Autowired
    private MaintainplanitemMapper maintainplanitemMapper;
    @Autowired
    private EqtteMapper eqtteMapper;
    @Autowired
    private EqttfMapper eqttfMapper;
    @Autowired
    private ZnxService znxService;
    /**
     * 生成模具 保养单 每天中午12点触发
     */
//    @Scheduled(cron = "0 0 12 * * ?")
//    @DSTransactional
//    public void maintainGen() {
//
//        List<Maintainplan> mpList = maintainplanMapper.mouldOnList();
//        for(Maintainplan plan : mpList) {
//            // 找到开启的规则。  找到时间要求最小的
//            Maintainplanrule rule = maintainplanruleMapper.selMinTime(plan.getMpuuid());
//            if(rule == null) continue;
//
//            // 先查出正在上机中的模具
//            List<Resource> list = resourceMapper.selUsingMould();
//            for(Resource t : list) {
//                long differenceInHours = TimeUtils.getDifferenceInHours(t.getMaintainTime(), TimeUtils.getFullNowTime());
//                if(rule.getMpruntimedays() > differenceInHours / 24) continue;
//
//                Maconlineinfo maconlineinfo = maconlineinfoMapper.selectOne(new LambdaQueryWrapper<Maconlineinfo>()
//                        .eq(Maconlineinfo::getMacno, t.getDeviceid())
//                        .eq(Maconlineinfo::getResourcetype, 3));
//                if(maconlineinfo == null) continue;
//
//                // 先更新MaintainTime
//                t.setMaintainTime(TimeUtils.getFullNowTime());
//                resourceMapper.update(t, new LambdaQueryWrapper<Resource>().eq(Resource::getDeviceid, t.getDeviceid()));
//
//                // 生成保养单
//                Eqtte eqtte = new Eqtte();
//                eqtte.setTE001("BYD");
//                eqtte.setTE002(eqtteMapper.selTe002(eqtte.getTE001()));
//                eqtte.setTE003(TimeUtils.HZStdTime());
//                eqtte.setTE004("1");
//                eqtte.setTE008("3");
//                eqtte.setTE010(maconlineinfo.getResourceId().toString());
//                eqtte.setTE011(maconlineinfo.getResourceno());
//                eqtte.setTE012(maconlineinfo.getResourcename());
//                eqtte.setTE019("0");
//                eqtte.setTE027("时间达到自动生成");
//                eqtte.setTE031(TimeUtils.HZStdTime());
//                eqtte.setTE032(TimeUtils.HZStdTime());
//                eqtte.setTE059(maconlineinfo.getMacid().toString());  // 现在模具在哪个机器ID
//                eqtte.setTE060(maconlineinfo.getMacno());
//                eqtte.setTE061(maconlineinfo.getMacname());
//                eqtte.setCREATE_DATE(TimeUtils.getFullNowTime());
//                eqtte.setCREATOR("ERP");
//                eqtteMapper.insert(eqtte);
//                // 获取单身的项目 通过planitem
//                List<Maintainplanitem> itemList = maintainplanitemMapper.selListByMpuuid(plan.getMpuuid());
//                int idx = 1;
//                for(Maintainplanitem item : itemList) {
//                    Eqttf eqttf = new Eqttf();
//                    eqttf.setTF001(eqtte.getTE001());
//                    eqttf.setTF002(eqtte.getTE002());
//                    eqttf.setTF003(""+idx);  idx++;
//                    eqttf.setUDF01(item.getMpiid());  // 维护编号
//                    eqttf.setUDF02(item.getMpiname());  // 维护名称
//                    eqttf.setTF005(item.getMpicontent());  // 维护内容
//                    eqttf.setTF008(item.getMpiresulttype());  // 结果类型 1.文字 2.数值 3.日期[DEF:1]
//                    eqttf.setCREATE_DATE(TimeUtils.getNowTime());
//                    eqttf.setCREATOR("ERP");
//                    eqttfMapper.insert(eqttf);
//                }
//
//                // 消息通知
//                znxService.saveZnxAndSendMessage(MessageTypeEnum.DIANJIAN.getValue(),
//                        "有一张保養单待处理, 单号：" + eqtte.getTE002(), ZnxStatusEnum.DAIBAN.getValue(), eqtte.getTE001()+":"+eqtte.getTE002());
//
//            }
//        }
//    }

}
